package basic50.fiftieth;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Basic50 {
    public static void main(String[] args) throws IOException {
        //题目：有五个学生，每个学生有3门课的成绩，从键盘输入以上数据（包括学生号，姓名，三门课成绩），
        // 计算出平均成绩，将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> students = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            System.out.println("Enter the student id: ");
            int id = sc.nextInt();
            System.out.println("Enter the student name: ");
            String name = sc.next();
            ArrayList<Course> courses = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                System.out.println("Enter the student courseName: ");
                String courseName = sc.next();
                System.out.println("Enter the student courseGrade: ");
                int courseGrade = sc.nextInt();
                Course course = new Course(courseName, courseGrade);
                courses.add(course);
            }
            Student s = new Student(id,name,courses);
            students.add(s);
        }
        File file = new File("stud.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (Student s : students) {
            int sum = 0;
            bw.write(String.valueOf(s.getId()));
            bw.newLine();
            bw.write(s.getName());
            bw.newLine();
            for (Course c : s.getCourses()) {
                bw.write(c.getName());
                bw.newLine();
                bw.write(String.valueOf(c.getGrade()));
                bw.newLine();
                sum += c.getGrade();
            }
            int avg = sum / s.getCourses().size();
            System.out.println( s.getName() + " averageGrade = "+avg);
            bw.write("平均分: "+avg);
        }
        bw.close();

    }
}

class Student {
    private int id;
    private String name;
    private ArrayList<Course> courses;

    public Student() {
    }
    public Student(int id, String name, ArrayList<Course> courses) {
        this.id = id;
        this.name = name;
        this.courses = courses;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public ArrayList<Course> getCourses() {
        return courses;
    }
    public void setCourses(ArrayList<Course> courses) {
        this.courses = courses;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", courses=" + courses +
                '}';
    }
}


class Course{
    private String name;
    private int grade;
    public Course() {}
    public Course(String name, int grade) {
        this.name = name;
        this.grade = grade;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "Course{" +
                "name='" + name + '\'' +
                ", grade=" + grade +
                '}';
    }
}
